package com.stx.test;
import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean running = true;

        while (running) {
            // 显示欢迎界面
            System.out.println("****************************");
            System.out.println("***欢迎使用计算器***");
            System.out.println("***请输入对应数字***");
            System.out.println("*****1、加法*******");
            System.out.println("*****2、减法*******");
            System.out.println("*****3、乘法*******");
            System.out.println("*****4、除法*******");
            System.out.println("*****5、退出*******");
            System.out.println("****************************");
            System.out.print("请选择操作（1-5）：");

            int choice = scanner.nextInt();
            switch (choice) {
                case 1:// 加法
                    addition(scanner);
                    break;
                case 2:// 减法
                    subtraction(scanner);
                    break;
                case 3:// 乘法
                    multiplication(scanner);
                    break;
                case 4:// 除法
                    division(scanner);
                    break;
                case 5:
                    System.out.println("感谢使用计算器，再见！");
                    running = false;
                    break;
                default:
                    System.out.println("输入错误，请输入1-5之间的数字！");
            }
            System.out.println(); // 空行分隔
        }
    }

    // 加法运算
    private static void addition(Scanner scanner) {
        System.out.println("进入加法运算");
        System.out.print("请输入第一个数字：");
        double num1 = scanner.nextDouble();
        System.out.print("请输入第二个数字：");
        double num2 = scanner.nextDouble();

        double result = num1 + num2;
        System.out.println("计算结果：" + num1 + " + " + num2 + " = " + result);
    }

    // 减法运算
    private static void subtraction(Scanner scanner) {
        System.out.println("进入减法运算");
        System.out.print("请输入第一个数字：");
        double num1 = scanner.nextDouble();
        System.out.print("请输入第二个数字：");
        double num2 = scanner.nextDouble();

        double result = num1 - num2;
        System.out.println("计算结果：" + num1 + " - " + num2 + " = " + result);
    }

    // 乘法运算
    private static void multiplication(Scanner scanner) {
        System.out.println("进入乘法运算");
        System.out.print("请输入第一个数字：");
        double num1 = scanner.nextDouble();
        System.out.print("请输入第二个数字：");
        double num2 = scanner.nextDouble();

        double result = num1 * num2;
        System.out.println("计算结果：" + num1 + " × " + num2 + " = " + result);
    }

    // 除法运算
    private static void division(Scanner scanner) {
        System.out.println("【除法运算】");
        System.out.print("请输入被除数：");
        double num1 = scanner.nextDouble();

        double num2 = scanner.nextDouble();
        if(num2 == 0){
            System.out.println("除数不能为0，请重新输入");
            num2 = scanner.nextDouble();
        }

        double result = num1 / num2;
        System.out.println("计算结果：" + num1 + " ÷ " + num2 + " = " + result);
    }
}